home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CUCD / Sound / PreludeAMP / src / layer3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-18  |  4.6 KB  |  194 lines

  1. /* this file is a part of amp software, (C) tomislav uzelac 1996,1997
  2. */
  3.  
  4. /* layer3.c  layer3 audio decoding
  5.  *
  6.  * Created by: tomislav uzelac  Mar  1 97
  7.  * Last modified by: 
  8.  */
  9. #include "amp.h"
  10. #include "audio.h"
  11. #include "dump.h"
  12. #include "getbits.h"
  13. #include "getdata.h"
  14. #include "huffman.h"
  15. #include "misc2.h"
  16. #include "rtbuf.h"
  17. #include "transform.h"
  18.  
  19. #define LAYER3
  20. #include "layer3.h"
  21.  
  22. /* this function decodes one layer3 audio frame, except for the header decoding
  23.  * which is done in main() [audio.c]. returns 0 if everything is ok.
  24.  */
  25. int layer3_frame(struct AUDIO_HEADER *header,int cnt)
  26. {
  27. static struct SIDE_INFO info;
  28.  
  29. int gr,ch,sb,i;
  30. int mean_frame_size,bitrate,fs,hsize,ssize;
  31.  
  32. /* we need these later, hsize is the size of header+side_info
  33. */
  34.     if (header->ID) 
  35.         if (header->mode==3) {
  36.             nch=1;
  37.             hsize=21;
  38.         } else {
  39.             nch=2;
  40.             hsize=36;
  41.         }
  42.     else
  43.         if (header->mode==3) {
  44.             nch=1;
  45.             hsize=13;
  46.         } else {
  47.             nch=2;
  48.             hsize=21;
  49.         }
  50.  
  51. /* crc increases hsize by 2
  52. */
  53.     if (header->protection_bit==0) hsize+=2;
  54.  
  55.  
  56. /* read layer3 specific side_info
  57. */
  58.         getinfo(header,&info);
  59.  
  60.  
  61. /* MPEG2 only has one granule
  62. */
  63.     bitrate=t_bitrate[header->ID][3-header->layer][header->bitrate_index];
  64.     fs=t_sampling_frequency[header->ID][header->sampling_frequency];
  65.         if (header->ID) mean_frame_size=144000*bitrate/fs;
  66.         else mean_frame_size=72000*bitrate/fs;
  67.  
  68.  
  69. /* check if mdb is too big for the first few frames. this means that
  70.  * a part of the stream could be missing. We must still fill the buffer
  71.  *
  72.  * don't forget to (re)initialise bclean_bytes to 0, and f_bdirty to FALSE!!!
  73.  */
  74.     if (f_bdirty) 
  75.         if (info.main_data_begin > bclean_bytes) {
  76.             fillbfr(mean_frame_size + header->padding_bit - hsize);
  77.             bclean_bytes+=mean_frame_size + header->padding_bit - hsize;
  78.             /* warn(" frame %d discarded, incomplete main_data\n",cnt); */
  79.             return 0;
  80.         } else {
  81.             /* re-initialise */
  82.             f_bdirty=FALSE;
  83.             bclean_bytes=0;
  84.         }
  85.         
  86. /* now update the data 'pointer' (counting in bits) according to
  87.  * the main_data_begin information
  88.  */
  89.         data = 8 * ((append - info.main_data_begin) & (BUFFER_SIZE-1));
  90.  
  91.  
  92. /* read into the buffer all bytes up to the start of next header
  93. */
  94.         fillbfr(mean_frame_size + header->padding_bit - hsize);
  95.  
  96.  
  97. /* these two should go away
  98. */
  99.     t_l=&t_b8_l[header->ID][header->sampling_frequency][0];
  100.     t_s=&t_b8_s[header->ID][header->sampling_frequency][0];
  101.  
  102. /* debug/dump stuff
  103. */
  104.     if (A_DUMP_BINARY) dump((int *)info.part2_3_length);
  105.  
  106. /* decode the scalefactors and huffman data
  107.  * this part needs to be enhanced for error robustness
  108.  */
  109.     for (gr=0;gr < ((header->ID) ? 2 : 1);gr++) {
  110.         for (ch=0;ch<nch;ch++) {
  111.             ssize=decode_scalefactors(&info,header,gr,ch);
  112.             decode_huffman_data(&info,gr,ch,ssize);
  113.         }
  114.         
  115.     /* requantization, stereo processing, reordering(shortbl)
  116.     */
  117.  
  118.         if (A_DOWNMIX && nch==2) requantize_downmix(gr,&info,header);
  119.         else 
  120.             if (header->mode!=1 || (header->mode==1 && header->mode_extension==0))
  121.                 for (ch=0;ch<nch;ch++) requantize_mono(gr,ch,&info,header);
  122.             else requantize_ms(gr,&info,header);
  123.  
  124.     /* just which window?
  125.     */
  126.  
  127.     /* this is a very temporary, very ugly hack. 
  128.     */
  129.         if (A_DOWNMIX) nch=1;
  130.  
  131.         for (ch=0; ch < (A_DOWNMIX ? 1:nch) ;ch++) {
  132.         int win_type; /* same as in the standard, long=0, start=1 ,.... */
  133.         int window_switching_flag = info.window_switching_flag[gr][ch];
  134.         int block_type = info.block_type[gr][ch];
  135.         int mixed_block_flag = info.mixed_block_flag[gr][ch];
  136.  
  137.         /* antialiasing butterflies
  138.         */
  139.             if (!(window_switching_flag &&
  140.                   block_type==2))
  141.                                 alias_reduction(ch);
  142.  
  143.             if (window_switching_flag &&
  144.                 block_type==2 &&
  145.                 mixed_block_flag)
  146.                 win_type=0;
  147.             else
  148.                     if (!window_switching_flag) win_type=0;
  149.                 else win_type=block_type;
  150.  
  151.         /* imdct ...
  152.         */
  153.             for (sb=0;sb<2;sb++)
  154.                 imdct(win_type,sb,ch);
  155.  
  156.             if (window_switching_flag &&
  157.                 block_type==2 &&
  158.                 mixed_block_flag)
  159.                 win_type=2;
  160.  
  161.         /* no_of_imdcts tells us how many subbands from the top are all zero
  162.          * it is set by the requantize functions in misc2.c
  163.          */
  164.             for (sb=2;sb<no_of_imdcts[ch];sb++)
  165.                 imdct(win_type,sb,ch);
  166.  
  167.             for (;sb<32;sb++) 
  168.                 for (i=0;i<18;i++) {
  169.                     res[sb][i]=s[ch][sb][i];
  170.                     s[ch][sb][i]=0.0f;
  171.                 }
  172.     
  173.         /* polyphase filterbank
  174.         */
  175.             /* if (nch == 2) this was a bug, tomislav */
  176.                     for (i=0;i<18;i++)
  177.                         poly(ch,i);
  178.         }
  179.  
  180.         printout();
  181.  
  182.         /* this is part2 of a particularily ugly hack. this should vanish as soon as nch isn't
  183.            a global variable anymore
  184.         */
  185.         if (A_DOWNMIX && header->mode!=3) nch=2;
  186.  
  187.     }    /*  for (gr... */ 
  188.  
  189.     /* return status: 0 for ok, errors will be added
  190.     */
  191.     return 0;
  192.  
  193.